package com.gzh.system.controller;


import com.gzh.core.model.vo.Result;
import com.gzh.core.service.CheckToken;
import com.gzh.core.service.LoginToken;
import com.gzh.core.util.CosUtils;
import com.gzh.core.util.NumericHelper;
import com.gzh.core.util.RequestContextUtil;
import com.gzh.system.model.vo.*;
import com.gzh.system.service.CosService;
import com.gzh.system.service.MailService;
import com.gzh.system.service.SmsService;
import com.gzh.system.service.SystemService;
import com.gzh.system.service.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


@Slf4j
@RequestMapping("/v1/api/sys")
@RestController
public class SystemController {

  @Autowired
  private MailService mailService;

  @Autowired
  private SmsService smsService;

  @Autowired
  private CosService cosService;

  @Autowired
  private SystemService systemService;

  @PostMapping("/sendMail")
  @LoginToken
  public ResponseEntity<?> sendMail(@RequestBody String mailTo) {
    return Result.ok(mailService.sendMail(mailTo));
  }

  @PostMapping("/remindMail")
  @LoginToken
  public ResponseEntity<?> remindMail(@RequestBody RemindInfo info) {
    mailService.remindMail(info);
    return Result.ok();
  }

  @PostMapping("/sendSms")
  public ResponseEntity<?> sendSms(@RequestBody  SendSmsParam param) {
    return Result.ok(smsService.sendSms(param.paramToIn()));
  }

  @GetMapping("/getImg")
  @CheckToken
  public ResponseEntity<?> getImg(ImgParam param) {
    List<ImgOut> list = cosService.getImg(param.paramToIn());
    return Result.ok(list);
  }

  @PostMapping("/upload")
  public ResponseEntity<?> upload(@RequestPart MultipartFile file,@RequestParam String bucketName,@RequestParam String prePath,@RequestParam String classify) {
    UploadParam param = new UploadParam();
    param.setBucketName(bucketName);
    param.setClassify(classify);
    param.setFile(file);
    param.setPrePath(prePath);
    UploadOut out = cosService.upload(param.paramToIn());
    return Result.ok(out);
  }

  @PostMapping("/downLoad")
  @CheckToken
  public void downLoad(@RequestBody DownLoadParam param,HttpServletResponse response) {
    cosService.downLoad(param.getFileId(),response);
  }

  @PostMapping("/deleteFile")
  @CheckToken
  public void deleteFile(@RequestBody DownLoadParam param) {
    cosService.deleteFile(param.getFileId());
  }


  @PostMapping("/readExcel")
  public void readExcel(@RequestPart MultipartFile file,@RequestParam("userId") String userId ) {
    systemService.readExcel(file, NumericHelper.toInteger(userId));
  }

  @PostMapping("/sendMailToCheck")
  public void sendMailToCheck(@RequestParam("courseId") Integer courseId,@RequestParam("checkId") Integer checkId) {
    systemService.sendMailToCheck(courseId,checkId);
  }
}
